﻿export default class SoftKey3W {
  static Socket_UK;
  static url;
  static LastError;
  constructor() {
    let isIE11 = navigator.userAgent.indexOf("Trident") > -1 && navigator.userAgent.indexOf("rv:11.0") > -1;
    let isEDGE = navigator.userAgent.indexOf("Edge") > -1;
    let u = document.URL;
    if (u.substring(0, 5) == "https") {
      if (isIE11 || isEDGE) {
        if (isIE11) this.url = "wss://127.0.0.1:4006/xxx";
        else url = "ws://127.0.0.1:4006/xxx";
      } else {
        this.url = "ws://localhost:4006/xxx";
      }
    } else {
      this.url = "ws://127.0.0.1:4006/xxx";
    }

    if (typeof MozWebSocket != "undefined") {
      this.Socket_UK = new MozWebSocket(this.url, "usbkey-protocol");
    } else {
      this.Socket_UK = new WebSocket(this.url, "usbkey-protocol");
    }
    this.Socket_UK.onerror = event => {
      // alert("未能连接服务程序，请确定服务程序是否安装。");
      console.log("event---", event);
    };
    SoftKey3W.LastError = 0;
  }

  _FindPort = function (UK, start) {
    let msg = {
      FunName: "FindPort",
      start: start
    };
    UK.send(JSON.stringify(msg));
  };

  _FindPort_2 = function (UK, start, in_data, verf_data) {
    let msg = {
      FunName: "FindPort_2",
      start: start,
      in_data: in_data,
      verf_data: verf_data
    };
    UK.send(JSON.stringify(msg));
  };

  _FindPort_3 = function (UK, start, in_data, verf_data) {
    let msg = {
      FunName: "FindPort_3",
      start: start,
      in_data: in_data,
      verf_data: verf_data
    };
    UK.send(JSON.stringify(msg));
  };

  _GetVersion = function (UK, Path) {
    let msg = {
      FunName: "GetVersion",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _GetVersionEx = function (UK, Path) {
    let msg = {
      FunName: "GetVersionEx",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _GetID_1 = function (UK, Path) {
    let msg = {
      FunName: "GetID_1",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _GetID_2 = function (UK, Path) {
    let msg = {
      FunName: "GetID_2",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };
  _sRead = function (UK, Path) {
    let msg = {
      FunName: "sRead",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _sWrite = function (UK, InData, Path) {
    let msg = {
      FunName: "sWrite",
      InData: InData,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _sWrite_2 = function (UK, InData, Path) {
    let msg = {
      FunName: "sWrite_2",
      InData: InData,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _sWrite_2Ex = function (UK, InData, Path) {
    let msg = {
      FunName: "sWrite_2Ex",
      InData: InData,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _sWriteEx = function (UK, InData, Path) {
    let msg = {
      FunName: "sWriteEx",
      InData: InData,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _sWriteEx_New = function (UK, InData, Path) {
    let msg = {
      FunName: "sWriteEx_New",
      InData: InData,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _sWrite_2Ex_New = function (UK, InData, Path) {
    let msg = {
      FunName: "sWrite_2Ex_New",
      InData: InData,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };
  _YRead = function (Address, HKey, LKey, Path) {
    let msg = {
      FunName: "YRead",
      Address: Address,
      HKey: HKey,
      LKey: LKey,
      Path: Path
    };
    Socket_UK.send(JSON.stringify(msg));
  };

  _YWrite = function (InData, Address, HKey, LKey, Path) {
    let msg = {
      FunName: "YWrite",
      InData: InData,
      Address: Address,
      HKey: HKey,
      LKey: LKey,
      Path: Path
    };
    Socket_UK.send(JSON.stringify(msg));
  };
  static _SetBuf = function (UK, InData, pos) {
    let msg = {
      FunName: "SetBuf",
      InData: InData,
      pos: pos
    };
    UK.send(JSON.stringify(msg));
  };

  static _GetBuf = function (UK, pos) {
    let msg = {
      FunName: "GetBuf",
      pos: pos
    };
    UK.send(JSON.stringify(msg));
  };

  static _YReadEx = function (UK, Address, len, HKey, LKey, Path) {
    let msg = {
      FunName: "YReadEx",
      Address: Address,
      len: len,
      HKey: HKey,
      LKey: LKey,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  static _YWriteEx = function (UK, Address, len, HKey, LKey, Path) {
    let msg = {
      FunName: "YWriteEx",
      Address: Address,
      len: len,
      HKey: HKey,
      LKey: LKey,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _YReadString = function (UK, Address, len, HKey, LKey, Path) {
    let msg = {
      FunName: "YReadString",
      Address: Address,
      len: len,
      HKey: HKey,
      LKey: LKey,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _YWriteString = function (UK, InString, Address, HKey, LKey, Path) {
    let msg = {
      FunName: "YWriteString",
      InString: InString,
      Address: Address,
      HKey: HKey,
      LKey: LKey,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _SetWritePassword = function (UK, W_Hkey, W_Lkey, new_Hkey, new_Lkey, Path) {
    let msg = {
      FunName: "SetWritePassword",
      W_Hkey: W_Hkey,
      W_Lkey: W_Lkey,
      new_Hkey: new_Hkey,
      new_Lkey: new_Lkey,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _SetReadPassword = function (UK, W_Hkey, W_Lkey, new_Hkey, new_Lkey, Path) {
    let msg = {
      FunName: "SetReadPassword",
      W_Hkey: W_Hkey,
      W_Lkey: W_Lkey,
      new_Hkey: new_Hkey,
      new_Lkey: new_Lkey,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };
  _DecString = function (UK, InString, Key) {
    let msg = {
      FunName: "DecString",
      InString: InString,
      Key: Key
    };
    UK.send(JSON.stringify(msg));
  };

  _EncString = function (UK, InString, Path) {
    let msg = {
      FunName: "EncString",
      InString: InString,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _EncString_New = function (UK, InString, Path) {
    let msg = {
      FunName: "EncString_New",
      InString: InString,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _Cal = function (UK, Path) {
    let msg = {
      FunName: "Cal",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _Cal_New = function (UK, Path) {
    let msg = {
      FunName: "Cal_New",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _GetProduceDate = function (UK, Path) {
    let msg = {
      FunName: "GetProduceDate",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _SetID = function (UK, Seed, Path) {
    let msg = {
      FunName: "SetID",
      Seed: Seed,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _SetCal_2 = function (UK, Key, Path) {
    let msg = {
      FunName: "SetCal_2",
      Key: Key,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _SetCal_2 = function (UK, Key, Path) {
    let msg = {
      FunName: "SetCal_2",
      Key: Key,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _SetCal_New = function (UK, Key, Path) {
    let msg = {
      FunName: "SetCal_New",
      Key: Key,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  static _SetEncBuf = function (UK, InData, pos) {
    let msg = {
      FunName: "SetEncBuf",
      InData: InData,
      pos: pos
    };
    UK.send(JSON.stringify(msg));
  };

  static _GetEncBuf = function (UK, pos) {
    let msg = {
      FunName: "GetEncBuf",
      pos: pos
    };
    UK.send(JSON.stringify(msg));
  };
  _ReSet = function (UK, Path) {
    let msg = {
      FunName: "ReSet",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _MacAddr = function (UK) {
    let msg = {
      FunName: "MacAddr"
    };
    UK.send(JSON.stringify(msg));
  };
  _GetChipID = function (UK, Path) {
    let msg = {
      FunName: "GetChipID",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  static _StarGenKeyPair = function (UK, Path) {
    let msg = {
      FunName: "StarGenKeyPair",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  static _GenPubKeyY = function (UK) {
    let msg = {
      FunName: "GenPubKeyY"
    };
    UK.send(JSON.stringify(msg));
  };

  static _GenPubKeyX = function (UK) {
    let msg = {
      FunName: "GenPubKeyX"
    };
    UK.send(JSON.stringify(msg));
  };

  static _GenPriKey = function (UK) {
    let msg = {
      FunName: "GenPriKey"
    };
    UK.send(JSON.stringify(msg));
  };

  _GetPubKeyY = function (UK, Path) {
    let msg = {
      FunName: "GetPubKeyY",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _GetPubKeyX = function (UK, Path) {
    let msg = {
      FunName: "GetPubKeyX",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _GetSm2UserName = function (UK, Path) {
    let msg = {
      FunName: "GetSm2UserName",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _Set_SM2_KeyPair = function (UK, PriKey, PubKeyX, PubKeyY, sm2UserName, Path) {
    let msg = {
      FunName: "Set_SM2_KeyPair",
      PriKey: PriKey,
      PubKeyX: PubKeyX,
      PubKeyY: PubKeyY,
      sm2UserName: sm2UserName,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _YtSign = function (UK, SignMsg, Pin, Path) {
    let msg = {
      FunName: "YtSign",
      SignMsg: SignMsg,
      Pin: Pin,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _YtSign_2 = function (UK, SignMsg, Pin, Path) {
    let msg = {
      FunName: "YtSign_2",
      SignMsg: SignMsg,
      Pin: Pin,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _YtVerfiy = function (UK, id, SignMsg, PubKeyX, PubKeyY, VerfiySign, Path) {
    let msg = {
      FunName: "YtVerfiy",
      id: id,
      SignMsg: SignMsg,
      PubKeyX: PubKeyX,
      PubKeyY: PubKeyY,
      VerfiySign: VerfiySign,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _SM2_DecString = function (UK, InString, Pin, Path) {
    let msg = {
      FunName: "SM2_DecString",
      InString: InString,
      Pin: Pin,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _SM2_EncString = function (UK, InString, Path) {
    let msg = {
      FunName: "SM2_EncString",
      InString: InString,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _YtSetPin = function (UK, OldPin, NewPin, Path) {
    let msg = {
      FunName: "YtSetPin",
      OldPin: OldPin,
      NewPin: NewPin,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _FindU = function (UK, start) {
    let msg = {
      FunName: "FindU",
      start: start
    };
    UK.send(JSON.stringify(msg));
  };

  _FindU_2 = function (UK, start, in_data, verf_data) {
    let msg = {
      FunName: "FindU_2",
      start: start,
      in_data: in_data,
      verf_data: verf_data
    };
    UK.send(JSON.stringify(msg));
  };

  _FindU_3 = function (UK, start, in_data, verf_data) {
    let msg = {
      FunName: "FindU_3",
      start: start,
      in_data: in_data,
      verf_data: verf_data
    };
    UK.send(JSON.stringify(msg));
  };

  _IsUReadOnly = function (UK, Path) {
    let msg = {
      FunName: "IsUReadOnly",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _SetUReadOnly = function (UK, Path) {
    let msg = {
      FunName: "SetUReadOnly",
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  _SetHidOnly = function (UK, IsHidOnly, Path) {
    let msg = {
      FunName: "SetHidOnly",
      IsHidOnly: IsHidOnly,
      Path: Path
    };
    UK.send(JSON.stringify(msg));
  };

  static ResetOrder = function (UK) {
    let msg = {
      FunName: "ResetOrder"
    };
    UK.send(JSON.stringify(msg));
  };

  ContinueOrder = function (UK) {
    let msg = {
      FunName: "ContinueOrder"
    };
    UK.send(JSON.stringify(msg));
  };
  _ComputerName = function (UK) {
    let msg = {
      FunName: "ComputerName"
    };
    UK.send(JSON.stringify(msg));
  };

  SendCmdAndWait = function (IsReturnErr, fun, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10) {
    let UK;

    return new Promise(resolve => {
      if (typeof MozWebSocket != "undefined") {
        UK = new MozWebSocket(this.url, "usbkey-protocol");
      } else {
        UK = new WebSocket(this.url, "usbkey-protocol");
      }

      try {
        UK.onopen = function () {
          SoftKey3W.ResetOrder(UK); //
        };

        UK.onmessage = function got_packet(Msg) {
          let UK_Data = JSON.parse(Msg.data);
          let return_value;
          if (UK_Data.type != "Process") return; //
          switch (UK_Data.order) {
            case 0:
              {
                fun(UK, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10);
              }
              break; //!!!!!
            case 1:
              {
                SoftKey3W.LastError = UK_Data.LastError;
                return_value = UK_Data.return_value;
                if (SoftKey3W.LastError != 0) {
                  if (IsReturnErr) return_value = SoftKey3W.LastError;
                }
                //
                UK.close();

                resolve(return_value);
              }
              break;
          }
        };
        UK.onclose = function () {};
        UK.onerror = event => {
          // alert("未能连接服务程序，请确定服务程序是否安装。");
          console.log("onerror----------", event);
        };
      } catch (e) {
        alert(e.name + ": " + e.message);
        resolve(false);
      }
    });
  };

  GetLastError = function () {
    return SoftKey3W.LastError;
  };

  FindPort = function (start) {
    return this.SendCmdAndWait(false, this._FindPort, start);
  };

  FindPort_2 = function (start, in_data, verf_data) {
    return this.SendCmdAndWait(false, this._FindPort_2, start, in_data, verf_data);
  };

  GetVersionEx = function (KeyPath) {
    return this.SendCmdAndWait(false, this._GetVersionEx, KeyPath);
  };

  GetVersion = function (KeyPath) {
    return this.SendCmdAndWait(false, this._GetVersion, KeyPath);
  };
  GetID_1 = function (KeyPath) {
    return this.SendCmdAndWait(false, this._GetID_1, KeyPath);
  };

  GetID_2 = function (KeyPath) {
    return this.SendCmdAndWait(false, this._GetID_2, KeyPath);
  };

  GetChipID = function (KeyPath) {
    return this.SendCmdAndWait(false, this._GetChipID, KeyPath);
  };

  SetWritePassword = function (W_HKey, W_LKey, new_HKey, new_LKey, KeyPath) {
    return this.SendCmdAndWait(true, this._SetWritePassword, W_HKey, W_LKey, new_HKey, new_LKey, KeyPath);
  };

  SetReadPassword = function (W_HKey, W_LKey, new_HKey, new_LKey, KeyPath) {
    return this.SendCmdAndWait(true, this._SetReadPassword, W_HKey, W_LKey, new_HKey, new_LKey, KeyPath);
  };
  SetCal_2 = function (Key, KeyPath) {
    return this.SendCmdAndWait(true, this._SetCal_2, Key, KeyPath);
  };

  SetCal_New = function (Key, KeyPath) {
    return this.SendCmdAndWait(true, this._SetCal_New, Key, KeyPath);
  };
  EncString = function (InString, KeyPath) {
    return this.SendCmdAndWait(false, this._EncString, InString, KeyPath);
  };

  EncString_New = function (InString, KeyPath) {
    return this.SendCmdAndWait(false, this._EncString_New, InString, KeyPath);
  };

  Cal = function (Inbuf, KeyPath) {
    return SubCal(_Cal, Inbuf, KeyPath);
  };

  Cal_New = function (Inbuf, KeyPath) {
    return SubCal(_Cal_New, Inbuf, KeyPath);
  };

  sWriteEx = function (in_data, KeyPath) {
    return this.SendCmdAndWait(false, this._sWriteEx, in_data, KeyPath);
  };

  sWrite_2Ex = function (in_data, KeyPath) {
    return this.SendCmdAndWait(false, this._sWrite_2Ex, in_data, KeyPath);
  };

  sWriteEx_New = function (in_data, KeyPath) {
    return this.SendCmdAndWait(false, this._sWriteEx_New, in_data, KeyPath);
  };

  sWrite_2Ex_New = function (in_data, KeyPath) {
    return this.SendCmdAndWait(false, this._sWrite_2Ex_New, in_data, KeyPath);
  };

  sWrite = function (in_data, KeyPath) {
    return this.SendCmdAndWait(true, this._sWrite, in_data, KeyPath);
  };

  sWrite_2 = function (in_data, KeyPath) {
    return this.SendCmdAndWait(true, this._sWrite_2, in_data, KeyPath);
  };

  sRead = function (KeyPath) {
    return this.SendCmdAndWait(false, this._sRead, KeyPath);
  };
  YWriteEx = function (indata, address, nlen, HKey, LKey, KeyPath) {
    let UK;

    return new Promise(resolve => {
      if (typeof MozWebSocket != "undefined") {
        UK = new MozWebSocket(this.url, "usbkey-protocol");
      } else {
        UK = new WebSocket(this.url, "usbkey-protocol");
      }

      try {
        let i = 0,
          order;
        console.log("i----", i);

        UK.onopen = function () {
          SoftKey3W.ResetOrder(UK); //
        };

        UK.onmessage = function got_packet(Msg) {
          let UK_Data = JSON.parse(Msg.data);
          let return_value;
          if (UK_Data.type != "Process") return; //
          if (UK_Data.order < nlen) {
            SoftKey3W._SetBuf(UK, indata[UK_Data.order], UK_Data.order);
            return;
          } else {
            order = UK_Data.order - nlen;
          }

          switch (order) {
            case 0:
              {
                SoftKey3W.LastError = UK_Data.LastError;
                if (SoftKey3W.LastError != 0) {
                  UK.close();
                  resolve(UK_Data.return_value);
                  return;
                }
                SoftKey3W._YWriteEx(UK, address, nlen, HKey, LKey, KeyPath);
              }
              break; //!!!!!
            case 1:
              {
                SoftKey3W.LastError = UK_Data.LastError;
                return_value = UK_Data.return_value;
                if (SoftKey3W.LastError != 0) {
                  return_value = SoftKey3W.LastError;
                }

                UK.close();

                resolve(return_value);
              }
              break;
          }
        };
        UK.onclose = function () {};
        UK.onerror = event => {
          // alert("未能连接服务程序，请确定服务程序是否安装。");
          console.log("onerror-event", event);
        };
      } catch (e) {
        alert(e.name + ": " + e.message);
        resolve(false);
      }
    });
  };
  YWriteString = function (InString, Address, HKey, LKey, KeyPath) {
    return this.SendCmdAndWait(true, this._YWriteString, InString, Address, HKey, LKey, KeyPath);
  };

  YReadEx = function (address, nlen, HKey, LKey, KeyPath) {
    let outb = new Uint8Array(nlen);
    let UK;
    return new Promise(resolve => {
      if (typeof MozWebSocket != "undefined") {
        UK = new MozWebSocket(this.url, "usbkey-protocol");
      } else {
        UK = new WebSocket(this.url, "usbkey-protocol");
      }

      try {
        let i, order;
        console.log("order----------", order);
        UK.onopen = function () {
          SoftKey3W.ResetOrder(UK); //
        };

        UK.onmessage = function got_packet(Msg) {
          let UK_Data = JSON.parse(Msg.data);
          let return_value;
          console.log("return_value----", return_value);

          if (UK_Data.type != "Process") return; //
          if (UK_Data.order < 2) {
            switch (UK_Data.order) {
              case 0:
                {
                  SoftKey3W._YReadEx(UK, address, nlen, HKey, LKey, KeyPath);
                }
                break; //!!!!!
              case 1:
                {
                  SoftKey3W.LastError = UK_Data.LastError;
                  if (SoftKey3W.LastError != 0) {
                    UK.close();
                    resolve(outb);
                    return;
                  }

                  i = 0;
                  SoftKey3W._GetBuf(UK, i); //
                }
                break;
            }
          } else {
            SoftKey3W.LastError = UK_Data.LastError;
            if (SoftKey3W.LastError != 0) {
              UK.close();
              resolve(outb);
              return;
            }
            outb[i] = UK_Data.return_value;
            i++;
            if (UK_Data.LastError != 0 || i >= nlen) {
              UK.close();
              resolve(outb);
              return;
            }
            SoftKey3W._GetBuf(UK, i); //
          }
        };
        UK.onclose = function () {};
        UK.onerror = event => {
          // alert("未能连接服务程序，请确定服务程序是否安装。");
          console.log("onerror:-----", event);
        };
      } catch (e) {
        alert(e.name + ": " + e.message);
        resolve(false);
      }
    });
    return outb;
  };

  YReadString = function (Address, nlen, HKey, LKey, KeyPath) {
    return this.SendCmdAndWait(false, this._YReadString, Address, nlen, HKey, LKey, KeyPath);
  };
  YRead = function (Address, HKey, LKey, Path) {
    return this.SendCmdAndWait(false, this._YRead, Address, HKey, LKey, Path);
  };

  YWrite = function (InData, Address, HKey, LKey, Path) {
    return this.SendCmdAndWait(true, this._YWrite, InData, Address, HKey, LKey, Path);
  };
  ReSet = function (KeyPath) {
    return this.SendCmdAndWait(true, this._ReSet, KeyPath);
  };
  SetCal = function (HKey, LKey, new_HKey, new_LKey, KeyPath) {
    return this.SendCmdAndWait(true, this._SetCal, HKey, LKey, new_HKey, new_LKey, KeyPath);
  };

  SetID = function (Seed, KeyPath) {
    return this.SendCmdAndWait(true, this._SetID, Seed, KeyPath);
  };

  GetProduceDate = function (KeyPath) {
    return this.SendCmdAndWait(false, this._GetProduceDate, KeyPath);
  };

  SetHidOnly = function (IsHidOnly, KeyPath) {
    return this.SendCmdAndWait(true, this._SetHidOnly, IsHidOnly, KeyPath);
  };
  SetUReadOnly = function (KeyPath) {
    return this.SendCmdAndWait(true, this._SetUReadOnly, KeyPath);
  };
  StarGenKeyPair = function (KeyPath) {
    let KeyPairInfo = {
      GenPriKey: "",
      GenPubKeyX: "",
      GenPubKeyY: ""
    };
    let UK;

    return new Promise(resolve => {
      if (typeof MozWebSocket != "undefined") {
        UK = new MozWebSocket(this.url, "usbkey-protocol");
      } else {
        UK = new WebSocket(this.url, "usbkey-protocol");
      }
      try {
        UK.onopen = function () {
          SoftKey3W.ResetOrder(UK); //
        };

        UK.onmessage = function got_packet(Msg) {
          let UK_Data = JSON.parse(Msg.data);
          let return_value;
          console.log("return_value:-----", return_value);
          if (UK_Data.type != "Process") return; //
          switch (UK_Data.order) {
            case 0:
              {
                SoftKey3W._StarGenKeyPair(UK, KeyPath);
              }
              break; //
            case 1:
              {
                SoftKey3W.LastError = UK_Data.LastError;
                if (SoftKey3W.LastError != 0) {
                  UK.close();
                  resolve(KeyPairInfo);
                  return;
                }
                SoftKey3W._GenPriKey(UK);
              }
              break;
            case 2:
              {
                SoftKey3W.LastError = UK_Data.LastError;
                if (SoftKey3W.LastError != 0) {
                  UK.close();
                  resolve(KeyPairInfo);
                  return;
                }
                KeyPairInfo.GenPriKey = UK_Data.return_value;
                SoftKey3W._GenPubKeyX(UK);
              }
              break;
            case 3:
              {
                SoftKey3W.LastError = UK_Data.LastError;
                if (SoftKey3W.LastError != 0) {
                  UK.close();
                  resolve(KeyPairInfo);
                  return;
                }
                KeyPairInfo.GenPubKeyX = UK_Data.return_value;
                SoftKey3W._GenPubKeyY(UK);
              }
              break;
            case 4:
              {
                SoftKey3W.LastError = UK_Data.LastError;
                if (SoftKey3W.LastError != 0) {
                  UK.close();
                  resolve(KeyPairInfo);
                  return;
                }
                KeyPairInfo.GenPubKeyY = UK_Data.return_value;

                UK.close();
                resolve(KeyPairInfo);
                return;
              }
              break;
          }
        };
        UK.onclose = function () {};
        UK.onerror = event => {
          // alert("未能连接服务程序，请确定服务程序是否安装。");
          console.log("UK.onerror--------", event);
        };
      } catch (e) {
        alert(e.name + ": " + e.message);
        resolve(false);
      }
    });
  };

  Set_SM2_KeyPair = function (PriKey, PubKeyX, PubKeyY, SM2_UserName, KeyPath) {
    return this.SendCmdAndWait(true, this._Set_SM2_KeyPair, PriKey, PubKeyX, PubKeyY, SM2_UserName, KeyPath);
  };

  Get_SM2_PubKey = function (KeyPath) {
    return this.SendCmdAndWait(false, this._Get_SM2_PubKey, KeyPath);
  };

  GetPubKeyX = function (KeyPath) {
    return this.SendCmdAndWait(false, this._GetPubKeyX, KeyPath);
  };

  GetPubKeyY = function (KeyPath) {
    return this.SendCmdAndWait(false, this._GetPubKeyY, KeyPath);
  };

  GetSm2UserName = function (KeyPath) {
    return this.SendCmdAndWait(false, this._GetSm2UserName, KeyPath);
  };

  SM2_EncBuf = function (InBuf, inlen, KeyPath) {
    return this.SendCmdAndWait(true, this._SM2_EncBuf, InBuf, inlen, KeyPath);
  };

  SM2_DecBuf = function (InBuf, inlen, pin, KeyPath) {
    return this.SendCmdAndWait(true, this._SM2_DecBuf, InBuf, inlen, pin, KeyPath);
  };

  SM2_EncString = function (InString, KeyPath) {
    return this.SendCmdAndWait(false, this._SM2_EncString, InString, KeyPath);
  };

  SM2_DecString = function (InString, pin, KeyPath) {
    return this.SendCmdAndWait(false, this._SM2_DecString, InString, pin, KeyPath);
  };

  YtSetPin = function (old_pin, new_pin, KeyPath) {
    return this.SendCmdAndWait(true, this._YtSetPin, old_pin, new_pin, KeyPath);
  };
  YtSign = function (msg, pin, KeyPath) {
    return this.SendCmdAndWait(false, this._YtSign, msg, pin, KeyPath);
  };

  YtSign_2 = function (msg, pin, KeyPath) {
    return this.SendCmdAndWait(false, this._YtSign_2, msg, pin, KeyPath);
  };

  MacAddr = function () {
    return this.SendCmdAndWait(false, this._MacAddr);
  };

  ComputerName = function () {
    return this.SendCmdAndWait(false, this._ComputerName);
  };

  SubCal = function (Fun, Inbuf, KeyPath) {
    let UK;
    let outb = new Uint8Array(8);
    return new Promise(resolve => {
      if (typeof MozWebSocket != "undefined") {
        UK = new MozWebSocket(this.url, "usbkey-protocol");
      } else {
        UK = new WebSocket(this.url, "usbkey-protocol");
      }

      try {
        let i = 0,
          order;
        let bIsEnc = true;
        UK.onopen = function () {
          SoftKey3W.ResetOrder(UK); //
        };

        UK.onmessage = function got_packet(Msg) {
          let UK_Data = JSON.parse(Msg.data);
          let return_value;
          if (UK_Data.type != "Process") return; //
          if (bIsEnc) {
            if (UK_Data.order < 8) {
              SoftKey3W._SetEncBuf(UK, Inbuf[UK_Data.order], UK_Data.order);
              return;
            } else {
              order = UK_Data.order - 8;
            }

            switch (order) {
              case 0:
                {
                  SoftKey3W.LastError = UK_Data.LastError;
                  if (SoftKey3W.LastError != 0) {
                    UK.close();
                    resolve(outb);
                    return;
                  }
                  Fun(UK, KeyPath);
                }
                break; //
              case 1:
                {
                  SoftKey3W.LastError = UK_Data.LastError;
                  return_value = UK_Data.return_value;
                  console.log("  return_value-------------", return_value);
                  if (SoftKey3W.LastError != 0) {
                    UK.close();
                    resolve(outb);
                    return;
                  }
                  bIsEnc = false;
                  i = 0;
                  SoftKey3W.ResetOrder(UK);
                }
                break;
            }
          } else {
            SoftKey3W.LastError = UK_Data.LastError;
            if (SoftKey3W.LastError != 0) {
              UK.close();
              resolve(outb);
              return;
            }
            if (UK_Data.order < 8) {
              if (UK_Data.order > 0) {
                outb[i - 1] = UK_Data.return_value;
              }
              SoftKey3W._GetEncBuf(UK, i); //
              i++;
            } else {
              outb[i - 1] = UK_Data.return_value;
              UK.close();
              resolve(outb);
              return;
            }
          }
        };
        UK.onclose = function () {};
        UK.onerror = event => {
          // alert("未能连接服务程序，请确定服务程序是否安装。");
          console.log(" UK.onerror ------", event);
        };
      } catch (e) {
        alert(e.name + ": " + e.message);
        resolve(false);
      }
    });
  };
}
